package suncertify.resources;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * A class that handles to I/O of the properties file for the application.
 * @author Paul Wilhelm
 *
 */
public class ConfigurationIO {
	
	/**
	 * Writes the properties to a file.  Creates the file if it doesn't exist.
	 * @throws IOException
	 */
	public static void writePropFile() throws IOException{
		FileWriter fw = new FileWriter(App.propertiesFile);
		BufferedWriter bw = new BufferedWriter(fw);
		bw.write(App.dbLocation + '\n');
		bw.write(App.host + '\n');
		bw.write(((Integer)App.port).toString() + '\n');
		bw.flush();
		bw.close();
	}
	
	/**
	 * Reads the properties from the configuration.properties file.
	 * @return A String[] of each element of the properties file
	 * @throws IOException
	 */
	public static String[] readPropFile() throws IOException{
		FileReader fr = new FileReader(App.propertiesFile);
		BufferedReader br = new BufferedReader(fr);
		String dbLocation = br.readLine();
		String host = br.readLine();
		String port = br.readLine();
		String[] propData = new String[] {dbLocation, host, port};
		return propData;
	}

}
